<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">

	<style type="text/css">
		@import url('../css/main.css');
	</style>

	<title>How to replace the default GRUB configuration file on a libreboot system</title>
</head>

<body>
	<div class="section">
		<h1 id="pagetop">How to replace the default GRUB configuration file on a libreboot system</h1>
			<p>
				Libreboot uses the GRUB <a href="http://www.coreboot.org/Payloads#GRUB_2">payload</a> 
				by default, which means that the GRUB configuration file 
				(where your GRUB menu comes from) is stored directly alongside libreboot
				and its GRUB payload executable, inside
				the flash chip. In context, this means that installing distributions and managing them 
				is handled slightly differently compared to traditional BIOS systems.
			</p>
			<p>
				A libreboot (or coreboot) ROM image is not simply &quot;flat&quot;; there is an actual 
				filesystem inside called CBFS (coreboot filesystem). A utility called 'cbfstool' 
				allows you to change the contents of the ROM image. In this case, libreboot is configured 
				such that the 'grub.cfg' and 'grubtest.cfg' files exist directly inside CBFS instead of 
				inside the GRUB payload 'memdisk' (which is itself stored in CBFS).
			</p>
			<p>
				You can either modify
				the GRUB configuration stored in the flash chip, or you can modify a GRUB configuration
				file on the main storage which the libreboot GRUB payload will automatically search for.
			</p>
			<p>
				Here is an excellent writeup about CBFS (coreboot filesystem): 
				<a href="http://lennartb.home.xs4all.nl/coreboot/col5.html">http://lennartb.home.xs4all.nl/coreboot/col5.html</a>.
			</p>
			<p>
				<a href="index.html">Back to previous index</a>
			</p>
	</div>

	<div class="section">

		<h1>Table of Contents</h1>

			<ul>
				<li><a href="#getting_started">Getting started</a></li>
				<li><a href="#libreboot_grub_config_ondisk">Option A: don't flash a new ROM</a></li>
				<li><a href="#libreboot_grub_config_flash">Option B: flash a new ROM</a></li>
				<ul>
					<li><a href="#tools">Get the tools ready</a></li>
					<ul>
						<li><a href="#locate_tools">Locate the tools in the binary release</a></li>
						<li><a href="#build_tools">Build the tools from source</a></li>
					</ul>
					<li><a href="#which_rom">Which ROM image should I use?</a></li>
					<ul>
						<li><a href="#use_prebuilt_rom">Use one of the provided ROM images</a></li>
						<li><a href="#re-use_rom">Re-use the currently flashed ROM image</a></li>
					</ul>
					<li><a href="#extract_grubtest">Extract grubtest from the ROM image</a>
					<li><a href="#reinsert_modified_grubtest">Re-insert the modified grubtest.cfg into the ROM image</a></li>
					<li><a href="#test_it">Test it!</a>
					<li><a href="#final_steps">Final steps</a></li>
					<li><a href="#troubleshooting">Troubleshooting</a></li>
				</ul>
			</ul>
			
	</div>

	<div class="section">

		<h2 id="getting_started">Getting started</h2>

			<p>
				Download the latest release from 
				<a href="http://libreboot.org/">http://libreboot.org/</a>
				<br/><b>If you downloaded from git, refer to 
				<a href="../git/index.html#build_meta">../git/index.html#build_meta</a> before continuing.</b>
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>

			<p>
				There are several advantages to modifying the GRUB configuration stored in CBFS, but
				this also means that you have to flash a new libreboot ROM image on your machine (some users
				feel intimidated by this, to say the least).
				Doing so can be risky if not handled correctly, because it can result in a bricked
				machine (recovery is easy if you have the <a href="../install/bbb_setup.html">equipment</a>
				for it, but most people don't). If you aren't up to that then don't worry; it is possible
				to use a custom GRUB menu without flashing a new image, by loading a GRUB configuration
				from a partition on the main storage instead.
			</p>

			
	</div>

	<div class="section">
		
		<h2 id="libreboot_grub_config_ondisk">Option A: don't flash a new ROM</h2>

			<p>
				By default, GRUB in libreboot is configured to scan all partitions on the main storage
				for /boot/grub/libreboot_grub.cfg or /grub/libreboot_grub.cfg(for systems where /boot
				is on a dedicated partition), and then use it automatically.
			</p>
			<p>
				Simply create your custom GRUB configuration and save it to <b>/boot/grub/libreboot_grub.cfg</b>
				on the running system. The next time you boot, GRUB (in libreboot) will automatically switch to
				this configuration file. <b>This means that you do not have to re-flash, recompile or otherwise
				modify libreboot at all!</b>
			</p>

			<p>
				Ideally, your distribution should automatically generate a libreboot_grub.cfg file that is written
				specifically under the assumption that it will be read and used on a libreboot system that uses
				GRUB as a payload. If your distribution does not do this, then you can try to add that feature
				yourself or politely ask someone involved with or otherwise knowledgeable about the distribution
				to do it for you. The libreboot_grub.cfg could either contain the full configuration, or it could
				chainload another GRUB ELF executable (built to be used as a coreboot payload) that is located in
				a partition on the main storage.
			</p>
			
			<p>
				If you want to adapt a copy of the existing <i>libreboot</i> GRUB configuration and use that for the libreboot_grub.cfg file, then
				follow <a href="#tools">#tools</a>, <a href="#which_rom">#which_rom</a> and
				<a href="#extract_grubtest">#extract_grubtest</a> to get the <b><i>grubtest.cfg</i></b>. 
				Rename <b><i>grubtest.cfg</i></b> to <b><i>libreboot_grub.cfg</i></b> and save it to <b><i>/boot/grub/</i></b>
				on the running system where it is intended to be used. Modify the file at that location however you see fit, 
				and then stop reading this guide (the rest of this page is irrelevant to you); <b>in libreboot_grub.cfg on disk,
				if you are adapting it based on grub.cfg from CBFS then remove the check for libreboot_grub.cfg otherwise it will loop.</b>.
			</p>

			<p>
				This is all well and good, but what should you actually put in your GRUB configuration file?
				Read <a href="grub_config.html">grub_config.html</a> for more information.
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">
		
		<h2 id="libreboot_grub_config_flash">Option B: flash a new ROM</h2>

			<p>
				Alternatively to editing the GRUB configuration file on your disk, you can also change the configuration that is
				stored in the CBFS on the flash chip.
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>
	<div class="section">
		<h2 id="tools">Get the tools ready</h2>

		<p>
			You will need to use the <b><i>cbfstool</i></b>, <b><i>rmodtool</i></b> and <b><i>flashrom</i></b> tools.
			You can either use the pre-compiled binaries provided by the binary release of libreboot, or build the tools
			yourself from source from the libreboot source code.
		</p>

		<div class="subsection">
			<h3 id="locate_tools">Locate the tools in the binary release</h3>

			<p>
				If you are working with the binary release libreboot_util, you will find the <b><i>cbfstool</i></b> and
				<b><i>rmodtool</i></b> binaries under ./cbfstool/{architecture}/ , where {architecture} refers to your 
				hardware name for which your current running kernel is compiled for. You can find it by running:
				<br/>
				<b>$ uname -m</b>
			</p>
			<p>
				The <b><i>flashrom</i></b> binary is located under ./flashrom/{architecture}/
			</p>
			<p>
				Whenever one of the tools is used in a command in this tutorial, it will be called as, e.g., ./cbfstool <br/>
				Please adapt the paths accordingly (e.g.: cbfstool/i686/cbfstool).
			</p>
		</div class="subsection">

		<div class="subsection">
			<h3 id="build_tools">Build the tools from source</h3>

			<p>
				If you are working with libreboot_src, then you can run <b><i>make</i></b> command in 
				libreboot_src/coreboot/util/cbfstool to build the <b><i>cbfstool</i></b> and <b><i>rmodtool</i></b>
				executables. See <a href="../git/index.html#build_flashrom">../git/index.html#build_flashrom</a> for
				instructions on how to build <b><i>flashrom</i></b>.

			</p>
		</div class="subsection">

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="which_rom">Which ROM image should I use?</h2>

			<p>
				You can either work directly with one of the ROM images already included in the libreboot ROM archives, or re-use the ROM that
				you have currently flashed. For the purpose of this tutorial it is assumed that your ROM image file is named <i>libreboot.rom</i>,
				so please make sure to adapt.
			</p>

			<div class="subsection">
				<h3 id="use_prebuilt_rom">Use one of the provided ROM images</h3>
				<p>
					Libreboot ROM images are distributed in separate tar archives, named {computer_model}.tar.xz.
					If you build from source without generated any release archives,
					the ROM images will be in ./bin/{computer_model}/. Make a working copy of the ROM you want to use:
					<br/>
					<b>$ cp ./bin/{computer_model}/{computer_model}_{keyboard_layout}_{mode}.rom libreboot.rom</b>
					<br/>
					{keyboard_layout} defines the keyboard layout that will be available on the GRUB console. This might be important
					if you protect GRUB with a password later on and want to insert non-ASCII characters.
					{mode} can be <i>vesafb</i> or <i>txtmode</i> and starts GRUB with a background image or in text mode.
				</p>
			</div>

			<div class="subsection">
				<h3 id="re-use_rom">Re-use the currently flashed ROM image</h3>
				<p>
					If you want to re-use the ROM that you currently have flashed (and running)				and then run:<br/>
					<b>$ sudo ./flashrom -p internal -r libreboot.rom</b><br/>
					Notice that this is using <b>&quot;-r&quot;</b> (read) instead of <b>&quot;-w&quot;</b> (write). 
					This will create a dump (copy) of your current firmware and name it <b>libreboot.rom</b>. 
					You need to take ownership of the file. For example:<br/>
					<b>$ sudo chown yourusername:yourusername libreboot.rom</b><br/>
					<b># chown yourusername:yourusername libreboot.rom</b>
				</p>
			</div>

			<p>
				If you currently have flashed a ROM image from an older version, it is recommended to update first: 
				basically, modify one of the latest ROM images and then flash it.
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="extract_grubtest">Extract grubtest.cfg from the ROM image</h2>

			<p>
				Display contents of ROM:<br/>
				<b>$ ./cbfstool libreboot.rom print</b>
			</p>

			<p>
				The libreboot.rom file contains your <i>grub.cfg</i> and <i>grubtest.cfg</i> files. 
				grub.cfg will load first, but it has a menu entry for switching to the copy (grubtest.cfg).
				Thus, you should extract, modify and re-insert the grubtest.cfg first. 
				This reduces your chance of making a mistake that could make your machine unbootable (or very hard to boot).
			</p>

			<p>
				Extract grubtest.cfg from the ROM image:<br/>
				<b>$ ./cbfstool libreboot.rom extract -n grubtest.cfg -f grubtest.cfg</b>
			</p>

			<p>
				Make a copy of the original file, before you modify it:<br/>
				<b>$ cp grubtest.cfg grubtest_vanilla.cfg</b>
			</p>

			<p>
				Now you have a grubtest.cfg in the cbfstool directory.
			</p>

			<p>
				This is all well and good, but what should you actually put in your GRUB configuration file?
				Read <a href="grub_config.html">grub_config.html</a> for more information.
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="reinsert_modified_grubtest">Re-insert the modified grubtest.cfg into the ROM image</h2>

			<p>
				So you've created your configuration. Now, how do you use it?
			</p>

			<p>
				Delete the grubtest.cfg that remained inside the ROM:<br/>
				<b>$ ./cbfstool libreboot.rom remove -n grubtest.cfg</b>
			</p>

			<p>
				Display ROM contents and now you see grubtest.cfg no longer exists there:<br/>
				<b>$ ./cbfstool libreboot.rom print</b>
			</p>

			<p>
				Add the modified version that you just made:<br/>
				<b>$ ./cbfstool libreboot.rom add -n grubtest.cfg -f grubtest.cfg -t raw</b>
			</p>

			<p>
				Now display ROM contents again and see that it exists again:<br/>
				<b>$ ./cbfstool libreboot.rom print</b>
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="test_it">Test it!</h2>

			<p>
				<b>
					Now you have a modified ROM. Refer back to <a href="../install/index.html#flashrom">../install/index.html#flashrom</a> for information
					on how to flash it. Once you have done that, shut down and then boot up with your new test configuration.
				</b>
			</p>

			<p>
				Choose (in GRUB) the menu entry that switches to grubtest.cfg. If it works, then your config is safe and you can continue below.
			</p>

			<p>
				<b>
					If it does not work like you want it to, if you are unsure or sceptical in any way, 
					then re-do the steps above until you get it right! Do *not* proceed past this point
					unless you are 100% sure that your new configuration is safe (or desirable) to use.
				</b>
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="final_steps">Final steps</h2>

			<p>
				Create a copy of grubtest.cfg, called grub.cfg, which is the same except for one difference: 
				change the menuentry 'Switch to grub.cfg' to 'Switch to grubtest.cfg' and inside it,
				change all instances of grub.cfg to grubtest.cfg. This is so that the main config still
				links (in the menu) to grubtest.cfg, so that you don't have to manually switch to it, in
				case you ever want to follow this guide again in the future (modifying the already modified config)<br/>
				$ <b>sed -e 's:(cbfsdisk)/grub.cfg:(cbfsdisk)/grubtest.cfg:g' -e 's:Switch to grub.cfg:Switch to grubtest.cfg:g' &lt; grubtest.cfg &gt; grub.cfg</b><br/>
			</p>

			<p>
				Delete the grub.cfg that remained inside the ROM:<br/>
				<b>$ ./cbfstool libreboot.rom remove -n grub.cfg</b>
			</p>

			<p>
				Display ROM contents and now you see grub.cfg no longer exists there:<br/>
				<b>$ ./cbfstool libreboot.rom print</b>
			</p>

			<p>
				Add the modified version that you just made:<br/>
				<b>$ ./cbfstool libreboot.rom add -n grub.cfg -f grub.cfg -t raw</b>
			</p>

			<p>
				Now display ROM contents again and see that it exists again:<br/>
				<b>$ ./cbfstool libreboot.rom print</b>
			</p>

			<p>
				<b>
					Now you have a modified ROM. Refer back to <a href="../install/index.html#flashrom">../install/index.html#flashrom</a> for information
					on how to flash it. Once you have done that, shut down and then boot up with your new configuration.
				</b>
			</p>

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<h2 id="troubleshooting">Troubleshooting</h2>

			<p>
				A user reported that segmentation faults occur with cbfstool 
				when using this procedure depending on the size of the grub.cfg being re-insterted. 
				In his case, a minimum size of 857 bytes was required. This could (at the time of 
				this release) be a bug in cbfstool that should be investigated with the coreboot 
				community. If cbfstool segfaults, then keep this in mind. 'strace' (or gdb? clang?) 
				could be used for debugging. This was in libreboot 5th release (based on coreboot 
				from late 2013), and I'm not sure if the issue persists in the current releases.
				I have not been able to reproduce it. strace (from that user) is here: 
				<a href="cbfstool_libreboot5_strace">cbfstool_libreboot5_strace</a>. 
				The issue has been reported by a few users, so it does not happen all the time: 
				this bug (if it still exists) could (should) be reproduced.
			</p> 

			<p>
				<a href="#pagetop">Back to top of page.</a>
			</p>
			
	</div>

	<div class="section">

		<p>
			Copyright &copy; 2014, 2015 Francis Rowe &lt;info@gluglug.org.uk&gt;<br/>
			Copyright &copy; 2015 Julian Mehne &lt;juli@nmehne.de&gt;<br/>
			This document is released under the Creative Commons Attribution-ShareAlike 4.0 International Public License and all future versions.
			A copy of the license can be found at <a href="../cc-by-sa-4.txt">../cc-by-sa-4.txt</a>.
		</p>

		<p>
			This document is distributed in the hope that it will be useful,
			but WITHOUT ANY WARRANTY; without even the implied warranty of
			MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See <a href="../cc-by-sa-4.txt">../cc-by-sa-4.txt</a> for more information.
		</p>
		
	</div>

</body>
</html>
